package com.lab.image.process;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;

import com.lab.logtrack.AppLog;


/**
 * 包含均值模糊。
 * Created by ak
 */
public class BlurProcessor implements IFProcessor{

    private static final int MAX_LENGTH = 100;

    public native void functionToBlur(Bitmap bitmapIn, Bitmap bitmapOut, int radius) ;

	public static boolean isLoadSuccess = false;
    static {
		try {
			System.loadLibrary("blur");
			isLoadSuccess = true;
		} catch (UnsatisfiedLinkError e) {
			isLoadSuccess = false;
		}
    }

    public boolean isLoadSuccess(){
        return isLoadSuccess;
    }

    public Bitmap blur(Bitmap original, float radius) {
        // modify by leo : recycled bitmap can't be used judgement
        // start
        if(null == original || original.isRecycled()) return null;
        // end
        Bitmap bitmap = resizeBitmap(original);
        //Create a copy
        Bitmap bitmapOut;
        //BlurProcess the copy
        if (!isLoadSuccess || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            bitmapOut = blurInJavaMethod(bitmap,(int)radius);
        }else{
            bitmapOut = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            functionToBlur(bitmap, bitmapOut, (int) radius);
        }
        return bitmapOut;
    }

    @Override
    public Bitmap process(Bitmap bitmap) {
		return blur(bitmap,16f);
    }

    private Bitmap resizeBitmap(Bitmap bitmap){
        if (bitmap.getHeight() > MAX_LENGTH || bitmap.getWidth() > MAX_LENGTH){
            bitmap = Bitmap.createScaledBitmap(bitmap,
                    MAX_LENGTH, MAX_LENGTH, true);
            return bitmap;
        }
        return bitmap;
    }

    public Bitmap blurInJavaMethod(Bitmap original, float rad) {
        int radius = (int) rad;
        if (radius < 1)
            radius = 1;

        long time = System.currentTimeMillis();
        int _width = original.getWidth();
        int _height = original.getHeight();
        boolean alpha = false;

        int[] currentPixels = new int[_width * _height];
        original.getPixels(currentPixels, 0, _width, 0, 0, _width, _height);
        int wm = _width - 1;
        int hm = _height - 1;
        int wh = _width * _height;
        int div = radius + radius + 1;

        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
        int vmin[] = new int[Math.max(_width, _height)];

        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int dv[] = new int[256 * divsum];
        for (i = 0; i < 256 * divsum; i++) {
            dv[i] = (i / divsum);
        }

        yw = yi = 0;

        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] sir;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;

        for (y = 0; y < _height; y++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = currentPixels[yi + Math.min(wm, Math.max(i, 0))];
                sir = stack[i + radius];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += sir[0] * rbs;
                gsum += sir[1] * rbs;
                bsum += sir[2] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
            }
            stackpointer = radius;

            for (x = 0; x < _width; x++) {
                if (!alpha){
                    //2014/12/1 original.getPixel nosuchmethod,这个方法支持api level1 所以加个catch
                    try {
                        alpha = Color.alpha(original.getPixel(x, y)) != 255;
                    }catch (Exception e){
                        AppLog.e(e.getMessage());
                    }
                }


                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (y == 0) {
                    vmin[x] = Math.min(x + radius + 1, wm);
                }
                p = currentPixels[yw + vmin[x]];

                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[(stackpointer) % div];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi++;
            }
            yw += _width;
        }
        for (x = 0; x < _width; x++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * _width;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + x;

                sir = stack[i + radius];

                sir[0] = r[yi];
                sir[1] = g[yi];
                sir[2] = b[yi];

                rbs = r1 - Math.abs(i);

                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;

                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }

                if (i < hm) {
                    yp += _width;
                }
            }
            yi = x;
            stackpointer = radius;
            for (y = 0; y < _height; y++) {
                // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                if (alpha)
                    currentPixels[yi] = (0xff000000 & currentPixels[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
                else
                    currentPixels[yi] = 0xff000000 | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (x == 0) {
                    vmin[y] = Math.min(y + r1, hm) * _width;
                }
                p = x + vmin[y];

                sir[0] = r[p];
                sir[1] = g[p];
                sir[2] = b[p];

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[stackpointer];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi += _width;
            }
        }
        return Bitmap.createBitmap(currentPixels, _width, _height, original.getConfig());
    }



}
